<?php
namespace Jammiest\SwigeCore\Console\Commands\Server;

use Symfony\Component\Console\Attribute\AsCommand;
use Jammiest\SwigeCore\Console\Commands\Command;
use Jammiest\SwigeCore\Tools\Path;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Swoole\Process;

#[AsCommand(
    name: 'server:status',
    description: '查看服务器状态'
)]
class StatusCommand extends Command
{
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $pidFile = Path::getBasePath('storage/swoole.pid');

        if (!file_exists($pidFile)) {
            $output->writeln('<error>服务器未运行</error>');
            return Command::FAILURE;
        }

        $pid = (int)file_get_contents($pidFile);

        if (Process::kill($pid, 0)) {
            $output->writeln(sprintf(
                '<info>服务器运行中 (PID: %d)</info>',
                $pid
            ));
            return Command::SUCCESS;
        }

        $output->writeln('<error>服务器未运行</error>');
        return Command::FAILURE;
    }
}